package com.example.day9_sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private MySqlite mySqlite;
    private SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mySqlite = new MySqlite(this);//实例化数据库
        database = mySqlite.getWritableDatabase();
    }

    public void insert(View view) {
        /*String name = "张三";
        database.execSQL("insert into user(name1,age) values('"+name+"',18)");*/
        /*Log.d("sss", "insert: "+name);*/
        ContentValues values = new ContentValues();
        values.put("name1","李四2");
        database.insert("user",null,values);
    }
    public void update(View view) {
        int id = 4;
        /*String name = "张三";

        database.execSQL("update user set name1='"+name+"' where id = "+id);*/
        ContentValues values = new ContentValues();
        values.put("age",25);
        database.update("user",values,"id=?",new String[]{id+""});
    }
    public void delete(View view) {
        int id = 3;
       /* database.execSQL("delete from user where id = "+id);*/
        database.delete("user","id=?",new String[]{id+""});
        //删除所有
        database.delete("user","",null);
    }
    @SuppressLint("Range")
    public void query(View view) {
       /* Cursor cursor = database.rawQuery("select * from user", null);*/
        Cursor cursor = database.query("user", null, null, null, null, null, "age desc");
        if(cursor!=null){
            //循环查询
            while (cursor.moveToNext()){
                 int id = cursor.getInt(cursor.getColumnIndex("id"));
                String name1 = cursor.getString(cursor.getColumnIndex("name1"));
                int age = cursor.getInt(cursor.getColumnIndex("age"));
                Log.d("sss", "query: id="+id+",name="+name1+",age="+age);
            }
            cursor.close();
        }
    }

    public void insertAll(View view) {
        long l = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            ContentValues values = new ContentValues();
            values.put("name1","李四100"+i);
            database.insert("user",null,values);
        }
        long l1 = System.currentTimeMillis();
        Log.d("sss", "insertAll: 使用时间"+(l1-l));

    }

    public void insertAllT(View view) {
        long l = System.currentTimeMillis();
        database.beginTransaction();
        for (int i = 0; i < 100; i++) {
            ContentValues values = new ContentValues();
            values.put("name1","李四200"+i);
            database.insert("user",null,values);
        }
        database.setTransactionSuccessful();//设置成功
        database.endTransaction();//结束事务
        long l1 = System.currentTimeMillis();
        Log.d("sss", "insertAllT: 使用时间"+(l1-l));

    }
}